package edu.hawaii.duedates;

import static org.junit.Assert.assertEquals; //import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import org.junit.Test;
import edu.hawaii.duedates.configuration.ConfigurationManager;
import edu.hawaii.duedates.library.Library;
import edu.hawaii.duedates.DueDates.SortType;
import edu.hawaii.duedates.book.Book;

/**
 * This will test the DueDates class.
 * 
 * New Teammembers: Vincent Leung, Creighton Okada, John Zhou
 * 
 * @author Ronn Reeves and Robin E. Raqueno
 */
public class TestDueDates {

  /**
   * Tests the constructor method.
   */
  @Test
  public void testConstructor() {
    DueDates dd = new DueDates();
    assertEquals("Library creation failed.", DueDates.class, dd.getClass());
  }

  /**
   * Test the email function.
   * 
   * 
   @Test public void testEmail() { DueDates dd = new DueDates();
   * dd.setSmtpServer("mail.hawaii.edu"); dd.setEmail("vleung@hawaii.edu");
   * 
   * String message = "Hello"; Boolean boo = dd.emailUser(message); // assertTrue("Email was sent.",
   * boo); // dd.setSmtpServer("hello"); // boo = dd.emailUser(message);
   * //assertFalse("Email failed to send.", boo); }
   */

  /**
   * Test get and set Email.
   */
  @Test
  public void testGetEmail() {
    DueDates dd = new DueDates();
    String email = "test1@hawaii.edu";
    dd.setEmail(email);
    assertEquals("Emails match.", email, dd.getEmail());
  }

  /**
   * Test get and set smtpServer.
   */
  @Test
  public void testGetSmtp() {
    DueDates dd = new DueDates();
    String smtp = "mail.hawaii.edu";
    dd.setSmtpServer(smtp);
    assertEquals("Emails match.", smtp, dd.getSmtpServer());
  }

  /**
   * Test requestInfo().
   */
  @Test
  public void testRequestInfo() {
    DueDates dd = new DueDates();
    Library library = new Library();
    library.setName("University of Hawaii");
    library.setLogin(System.getenv("DUEDATES_TESTUSER1_ID"));
    library.setPassword(System.getenv("DUEDATES_TESTUSER1_LASTNAME"));
    library.setLocation("edu.hawaii.library.uh.UhLibrary");
    try {
      dd.setLibrary(library);
      dd.requestInfo();
      assertTrue("RequestInfo() failed.", (dd.getBooks().size() >= 0));
    }
    catch (Exception e) {
      fail("RequestInfo() failed.");
    }
  }

  /**
   * Test emailUser.
   * 
   * @Test public void testEmail() { DueDates dd = new DueDates(); dd.setEmail("vleung@hawaii.edu");
   * dd.setSmtpServer("mail.hawaii.edu"); Boolean boo = dd.emailUser();
   * assertTrue("Email was sent.", boo);
   * 
   * dd.setEmail("test1@hawaii.edu"); boo = dd.emailUser(); assertFalse("Email failed.", boo);
   * 
   * }
   */
  /**
   * Test requestInfo.
   */
  @Test
  public void testGetInfo() {
    File exampleDueDatesXml = new File(System.getProperty("user.dir") + "/xml/duedates.xml");
    File exampleDueDatesXsd = new File(System.getProperty("user.dir")
        + "/xml/duedates.definition.xsd");
    ConfigurationManager manager = new ConfigurationManager(exampleDueDatesXml, exampleDueDatesXsd);
    DueDates dd = manager.getDueDates("test2@hawaii.edu", "test2now");
    dd.setWithin(5);
    try {
      dd.requestInfo();
      List<Book> books;
      books = dd.getBooks();
      assertNotNull("Books list not null.", books);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Test toString.
   */
  @Test
  public void testToString() {
    DueDates dd = new DueDates();
    assertEquals("String equals test.", dd.toString(), "Hello");
  }

  /**
   * Test getWithin.
   */
  @Test
  public void testGetWithin() {
    DueDates dd = new DueDates();
    dd.setWithin(5);
    int i = 5;
    assertEquals("Within is 5.", i, dd.getWithin());
  }

  /**
   * Test email User.
   */
  @Test
  public void testEmailUser() {
    DueDates dd = new DueDates();
    dd.setEmail("vleung@hawaii.edu");
    dd.setSmtpServer("mail.hawaii.edu");
    assertTrue("Email was sent.", dd.emailUser());
  }

  /**
   * Test sort.
   */
  @Test
  public void testSort() {
    List<Book> testbooks = new ArrayList<Book>();
    DueDates testdd = new DueDates();

    Calendar today = Calendar.getInstance(Locale.US);
    Calendar tomorrow = Calendar.getInstance(Locale.US);
    Calendar dayAfterTomorrow = Calendar.getInstance(Locale.US);
    tomorrow.add(Calendar.DAY_OF_MONTH, 1);
    dayAfterTomorrow.add(Calendar.DAY_OF_MONTH, 2);
    Book book1 = new Book("Book2", tomorrow, "Library A");
    Book book2 = new Book("Book1", dayAfterTomorrow, "Library B");
    Book book3 = new Book("Book3", today, "Library C");
    testbooks.add(book1);
    testbooks.add(book2);
    testbooks.add(book3);
    testdd.setBooks(testbooks);

    SortType sortOption = SortType.DUE_DATE;
    testdd.sort(sortOption);
    assertEquals("Books are sorted by due date", book3, testdd.getBooks().get(0));

    sortOption = SortType.LIBRARY;
    testdd.sort(sortOption);
    assertEquals("Books are sorted by library", book1, testdd.getBooks().get(0));

    sortOption = SortType.TITLE;
    testdd.sort(sortOption);
    assertEquals("Books are sorted by title", book2, testdd.getBooks().get(0));

    // testdd.setWithin(1);
    // testdd.sort();
    // assertEquals("Number of books is smaller", testdd.getBooks().size(), 2);
  }

}